<template>
	<view class="page">
		<block
		v-if="loginStatus"
		>
		<view class="user-info">
			<image :src="userInfo.avatarUrl" mode="scaleToFill"></image>
			<view class="user-profile">
				<text class="name">{{userInfo.nickName}}</text>
				<text class="country">{{userInfo.country}}</text>
			</view>
		</view>
			<view
			v-for="(i, t) in list" 
			:key="t"
			class="item">
				<mp-slideview 
				:buttons="i.lock?slideButtons:slideButtonsUnloack" 
				@buttontap="(data) => handleSlide(data)">
					<view class="item-main">
						<view class="title">
							{{i.title}}
						</view>
						<view class="desc">
							{{i.content}}
						</view>
						<image 
						v-if="i.lock"
						class="lock"
						src="../../static/lock.png"></image>
					</view>
				</mp-slideview>
			</view>
		</block>
		<view 
		v-else
		class="login">
			<button @click="getProfile()" size="mini">微信授权登录</button>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'list',
		data() {
			return {
				slideButtons: [{
						text: "解锁",
						extClass: "blue"
					},
					{
						text: "删除",
						extClass: "red"
					},
				],
				slideButtonsUnloack: [{
						text: "上锁",
						extClass: "blue"
					},
					{
						text: "删除",
						extClass: "red"
					},
				]
			};
		},
		computed: {
			loginStatus() {
				return this.$store.state.global.isLogin
			},
			userInfo() {
				return this.$store.state.global.userinfo
			},
			list() {
				return this.$store.state.global.list
			}
		},
		onShow() {
			this.getList()
		},
		methods: {
			handleSlide: function(val) {
				console.log(val);
			},
			getList() {
				const db = wx.cloud.database();
				db.collection('todos').where({
					_openid:'{openid}'
				}).get({
					success: res => {
						if (res.data.length!=0) {
							this.$store.commit('global/setList', res.data)
						}
					},
					fail: err => {
						console.log('ERROR', err)
					}
				})
			},
			getProfile() {
				wx.getUserProfile({
					desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
					success: (res) => {
						// console.log(res.userInfo)
						this.onRegeister(res.userInfo)
					},
					fail: err => {
						console.log('用户信息获取失败！', err)
					}
				})
			},
			onRegeister(userinfo) {
				const db = wx.cloud.database();
				db.collection('users').add({
					data: {
						...userinfo,
					},
					success: (res) => {
						this.$store.commit('global/setUserinfo', res.data)
						this.$store.commit('global/setLoginStatus', true)
						wx.showToast({
							title: '注册成功！',
						})
					},
					fail: err => {
						wx.showToast({
							icon: 'none',
							title: '注册失败!'
						})
					}
				})
			},
			onLogin() {
				// const _openid = this._openid
				const db = wx.cloud.database();
				db.collection('users').where({
					_openid:'{openid}'
				}).get({
					success: res => {
						if (res.data.length != 0) {
							wx.showToast({
								title: '登录成功！',
							})
							this.$store.commit('global/setUserinfo', res.data[0])
							this.$store.commit('global/setLoginStatus', true)
							// uni.navigateTo({
							// 	url: '../list/index'
							// })
						}
					},
				})
			},
		},
		onLoad() {
			this.onLogin()
		}
	}
</script>

<style lang="less">
	page {
		background-color: #fff;
	}

	.page {
		padding-top: 200rpx;
	}

	.item-main {
		padding: 20rpx 30rpx;
		position: relative;
		.title {
			font-size: 32rpx;
			color: #000;
			white-space: normal;
			text-overflow: ellipsis;
			overflow: hidden;
			padding-right: 60rpx;
		}
		.desc {
			font-size: 28rpx;
			color: #999;
			white-space: normal;
			text-overflow: ellipsis;
			overflow: hidden;
			padding-right: 60rpx;
		}
		.lock {
			position: absolute;
			width: 30rpx;
			height: 30rpx;
			right: 20rpx;
			top: 50%;
			transform: translateY(-50%);
		}
	}
	.login{
		text-align: center;
		padding-top: calc(50%);
	}
	.user-info {
		position: fixed;
		width: 100%;
		top: 0;
		display: flex;
		padding: 60rpx 30rpx 20rpx;
		align-items: center;
		box-shadow: 0 6rpx 16rpx -8rpx #ffffff14, 0 9rpx 28rpx #ffffff0d, 0 12rpx 48rpx 1;
		image {
			width: 120rpx;
			height: 120rpx;
			border-radius: 60rpx;
		}
		.user-profile {
			display: flex;
			flex-direction: column;
			padding-left: 30rpx;
			text.name {
				font-size: 30rpx;
			}
			text.country {
				font-size: 24rpx;
			}
		}
	}
	mp-slideview .blue .weui-slideview__btn {
		background-color: #10aeff;
	}

	mp-slideview .red .weui-slideview__btn {
		background-color: #fa5151;
	}
</style>
